<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="充值方式" prop="payType">
        <el-select v-model="queryParams.payId" placeholder="充值方式" @keyup.enter.native="handleQuery" clearable>
          <el-option v-for="(item, index) in types" :key="index" :label="item.typeName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="充值方式" @keyup.enter.native="handleQuery" clearable>
          <el-option v-for="(item, index) in payStatusOptions" :key="index" :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker v-model="dateRange" style="width: 320px" type="datetimerange" align="right"
          start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:recharge:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rechargeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="预存单号" align="center" prop="orderSn" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="预存金额" align="center" prop="account" />
      <el-table-column label="充值方式" align="center" prop="pyName" />
      <el-table-column label="支付状态" align="center">
        <template slot-scope="scope">
          <p>{{ payStatus[scope.row.payStatus] }}</p>
        </template>
      </el-table-column>
      <el-table-column label="预存奖励" align="center" prop="reward">
        <template slot-scope="scope">
          <p>{{ scope.row.rewardType == 0 ? (scope.row.reward == 100 ? '无' : (scope.row.reward + ' 折')) :
      scope.row.reward }}</p>
        </template>
      </el-table-column>

      <el-table-column label="充值时间" align="center" prop="createTime">

        <template slot-scope="scope">
          <p>{{ timestampToDate(scope.row.createTime) }}</p>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:recharge:changePayment']" v-if="scope.row.payStatus == 0">变更支付</el-button>
          <el-button size="mini" type="text" @click="handleShowPlace(scope.row)" v-if="scope.row.rewardType
      == 1" v-hasPermi="['finance:recharge:deliveryAdress']">收货地址</el-button>
        </template>
      </el-table-column>
    </el-table>

    <changePayment title="变更支付状态" iptLable="预存金额" :requestURL="'/system/recharge/changePayment'"
      :visiable="paymentVisiable" :Obj="currentRow" column="payStatus" @close="closeChangePayment"
      @editOperationStatus="confirmChangePayment" :options="payStatusOptions" :payList="types" payColumn="payId"></changePayment>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="收货地址" :visible.sync="placeVisiable" width="600px" append-to-body>
      <el-table :data="addressInfo">
        <el-table-column label="收货人" align="center" prop="consignee" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="所在地区" align="center">

          <template slot-scope="scope">
            {{ scope.row.prov }} {{ scope.row.city }} {{ scope.row.distinguish }}
          </template>
        </el-table-column>
        <el-table-column label="详细地址" align="center" prop="address" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecharge,
  getRecharge,
  delRecharge,
  addRecharge,
  updateRecharge
} from "../../../api/finance/recharge";
import { PRE_STORED_REWARDS, RECHARGE_PARAM, RECHARGE_STATUS, REWARD_STATUS_OPTIONS } from "../../../constant/const";
import { listPayType, listPayTypeJSON } from '@/api/configManager/payType'
import { timestampToDate } from "@/utils/time"

export default {
  name: "Recharge",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      payStatus: RECHARGE_STATUS,
      // 显示搜索条件
      showSearch: true,
      // 是否是搜索状态
      isSearch: false,
      // 总条数
      total: 0,
      // 充值记录 表格数据
      rechargeList: [],
      dateRange: undefined,
      currentRow: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      paymentVisiable: false,
      rewards: PRE_STORED_REWARDS,
      // 查询参数
      queryParams: { ...RECHARGE_PARAM },
      types: [],
      payStatusOptions: REWARD_STATUS_OPTIONS,
      //展示收货地址
      showAddress: false,
      placeVisiable: false,
      addressInfo: []
    };
  },
  created() {
    this.getList();
  },

  mounted() {
    listPayType(this).then(res => {
      this.types = this.types && this.types.filter(item => item.id !== 4)
    });

  },

  components: {
    changePayment: () => import("../../../components/payManger/changePayment.vue")
  },
  methods: {
    timestampToDate,
    /** 查询充值记录 列表 */
    getList() {
      this.loading = true;
      let params = {}
      if (this.isSearch) {
        params = Object.assign({}, this.queryParams);
        params.startTime = this.dateRange ? new Date(this.dateRange[0]).getTime() / 1000 : ''
        params.endTime = this.dateRange ? new Date(this.dateRange[1]).getTime() / 1000 : ''
      } else {
        params = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
        }
      }

      listRecharge(params).then(response => {
        this.rechargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderSn: null,
        account: null,
        createTime: null,
        payTime: null,
        payId: null,
        payStatus: null,
        payType: null,
        rewardType: null
      };
      this.resetForm("form");
    },
    /**
     * 关闭变更支付
     * @param {Boolean} visiable 
     */
    closeChangePayment(visiable) {
      this.paymentVisiable = visiable;
    },
    confirmChangePayment(e) {
      this.getList();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.isSearch = true;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.isSearch = false;
      this.resetForm("queryForm");
      this.dateRange = undefined;
      this.queryParams = { ...RECHARGE_PARAM };
      this.queryParams.pageNum = 1;
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值记录 ";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.currentRow = row;
      this.paymentVisiable = true;

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecharge(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecharge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充值记录 编号为"' + ids + '"的数据项？').then(function () {
        return delRecharge(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/recharge/export', {
        ...this.queryParams,
        ids: this.ids.length? this.ids : undefined
      }, `recharge_${new Date().getTime()}.xlsx`)
    },
    //展示收货地址
    handleShowPlace(item) {
      this.addressInfo = [item];
      this.placeVisiable = true;
    }
  }
};
</script>